<?php require VIEWPATH . ADMIN_DIR . DIRECTORY_SEPARATOR . 'header.php' ?>
<style type="text/css">
	.table-lesson {
		font-size: 14px;
	}
	.table-lesson th{
		text-align: center;
		width: 10%;
	}
	.table-lesson tbody tr td{
		text-align: center;
		color: #2679B5;
		width: 10%;
	}
</style>
<div class="page-header">
	<h1 style="font-size: 20px;">
		<a class="a-back" href="<?= admin_url('contents/schedule') ?>">
            <i class="fa fa-reply"></i>
		</a>
		<?= $classInfo['class_name'] ?>&nbsp;课程表
	</h1>
</div>
<div class="col-xs-12">
	<table class="table table-striped table-bordered table-hover table-lesson">
		<thead>
			<tr>
				<th width="">上课时间</th>
				<th width="">星期一</th>
				<th width="">星期二</th>
				<th width="">星期三</th>
				<th width="">星期四</th>
				<th width="">星期五</th>
				<?php if($schedule['schedule_week_days'] > 5): ?>
					<th width="">星期六</th>
				<?php endif ?>
				<?php if($schedule['schedule_week_days'] > 6): ?>
					<th width="">星期日</th>
				<?php endif ?>
			</tr>
		</thead>
		<tbody>
			<?php foreach($schedule['timeArr'] as $key => $time): ?>
				<tr>
					<th style="font-weight: normal;">
						第<?= $key + 1 ?>节
						<br/>
						<?= $time['beginTime'] ?>&nbsp; - &nbsp;<?= $time['endTime'] ?>
					</th>
					<td data-weekindex="1" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<td data-weekindex="2" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<td data-weekindex="3" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<td data-weekindex="4" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<td data-weekindex="5" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<?php if($schedule['schedule_week_days'] > 5): ?>
						<td data-weekindex="6" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<?php endif ?>
					<?php if($schedule['schedule_week_days'] > 6): ?>
						<td data-weekindex="7" data-classindex="<?= $key + 1 ?>" href="#modal-lesson" data-toggle="modal"><i class="fa fa-pencil"></i></td>
					<?php endif ?>
				</tr>
			<?php endforeach ?>
		</tbody>
	</table>
</div>
<!-- 编辑课程弹窗 -->
<div id="modal-lesson" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					编辑课程
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10">
							<input type="hidden" name="lesson_id" />
							<!-- 当前编辑是星期几 -->
							<input type="hidden" name="weekindex" />
							<!-- 当前编辑是第几节课 -->
							<input type="hidden" name="classindex" />
							<!-- 任课老师提交输入框 -->
							<!-- <input type="hidden" name="teacher_id" value='<?= isset($lesson['outside_teachers']) ? $room['outside_teachers'] : '' ?>' /> -->
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">科目：</label>
								<div class="col-sm-6">
									<select class="form-control" name="subject">
										<?php foreach($subjects as $subject): ?>
											<option value="<?= $subject['lesson_name'] ?>" data-id="<?= $subject['id'] ?>">
												<?= $subject['lesson_name'] ?>
											</option>
										<?php endforeach ?> 
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">任课教师：</label>
								<span class="block input-icon input-icon-right pd0">
									<div class="col-sm-6">
										<select class="form-control" name="teacher">
											<?php foreach($coachList as $coach): ?>
													<option value="<?= $coach['teacher_id'] ?>" data-subjectids="<?= $coach['subject_ids'] ?>">
														<?= $coach['realname'] ?>
													</option>
											<?php endforeach ?> 
										</select>
									</div>
								</span>
							</div>
							<!-- <div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">任课教师：</label>
								<span class="block input-icon input-icon-right pd0">
									<div class="col-sm-6">
										<span class="block input-icon input-icon-right pd0">
											<input class="width-100" type="text" placeholder="请选择教师" id="select-teacher" name="teacher_name" />
											<i class="ace-icon fa fa-user"></i>
										</span>
									</div>
								</span>
							</div> -->
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">上课课室：</label>
								<div class="col-sm-6">
									<select class="form-control" name="classroom">
										<?php foreach($classrooms as $classroom): ?>
											<option value="<?= $classroom['id'] ?>"<?php if(isset($room['lesson_id']) && $subject['id'] == $room['lesson_id']): ?>selected<?php endif ?>>
												<?= $classroom['classroom_name'] ?>
											</option>
										<?php endforeach ?> 
									</select>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-danger" id="delete-lesson" style="margin-right: 10px;">
					<i class="ace-icon fa fa-times"></i> 删除
				</a>
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-lesson">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function() {
		$("body").on("click", ".table-lesson tbody tr td", function() {
			var weekIndex = $(this).data("weekindex");
			var classIndex = $(this).data("classindex");
			var lessonId = $(this).data('lessonid');
			var subject = $(this).data('subject');
			var teacherId = $(this).data('teacherid');
			var teacherName = $(this).data('teachername');
			var classroomId = $(this).data('classroomid');
			// 设置隐藏input的值
			$("[name=weekindex]").val(weekIndex);
			$("[name=classindex]").val(classIndex);
			if(lessonId !== undefined){
				$("[name=lesson_id]").val(lessonId);
				$("[name=subject]").val(subject);
				$("[name=teacher_id]").val(teacherId);
				$("[name=teacher_name]").val(teacherName);
				$("[name=classroom]").val(classroomId);
				// 修改情况，显示删除按钮
				$("#delete-lesson").show();
			} else {
				// 添加情况，隐藏删除按钮
				$("#delete-lesson").hide();
			}
			handleCoach();
			$("[name=teacher]").val('');
			$("[name=teacher] option").each(function() {
				if(!$(this).hasClass('hidden') && $(this).val() == teacherId) {
					$(this).attr("selected", true);
				}
			});
			// $("[name=teacher]").(teacherName);
		});
		$("[name=subject]").change(function() {
			// var teacherId = $(this).data('teacherid');
			handleCoach();
			$("[name=teacher]").val('');
			$("[name=teacher] option").each(function() {
				// if(!$(this).hasClass('hidden') && $(this).val() == teacherId) {
				// 	$(this).attr("selected", true);
				// }
				if(!$(this).hasClass('hidden')) {
					$(this).attr("selected", true);
				}
			});
		});
		// 保存课程
		$("#save-lesson").click(function() {
			var weekIndex = $("[name=weekindex]").val();
			var classIndex = $("[name=classindex]").val();
			var lessonId = $("[name=lesson_id]").val();
			var subject = $("[name=subject]").val();
			// var teacherId = $("[name=teacher_id]").val();
			var teacherId = $("[name=teacher]").val();
			var classroomId = $("[name=classroom]").val();
			var posturl = "<?= admin_url('schedule/save_schedule_lesson_action') ?>";
			var postdata = {
				'schedule_id': <?= $schedule['id'] ?>,
				'class_id': <?= $classInfo['id'] ?>,
				'week_index': weekIndex,
				'class_index': classIndex,
				'lesson_id': lessonId,
				'subject': subject,
				'teacher_id': teacherId,
				'classroom_id': classroomId
			};
			ajax_post(posturl, postdata, function() {
				// 刷新页面
				window.location.href = "<?= admin_url('schedule/save_schedule_lesson?schedule_id=' . $schedule['id'] . '&class_id=' . $classInfo['id']) ?>";	
			});
		});
		// 删除课程
		$("#delete-lesson").click(function(){
			var lessonId = $("[name=lesson_id]").val();
			var subject = $("[name=subject]").val();
			var posturl = "<?= admin_url('schedule/delete_schedule_lesson_action') ?>";
			layer.confirm('确定删除课程：' + subject + '吗？', { title:"提示", offset: '130px' }, function(){
				ajax_post(posturl, { "lesson_id": lessonId }, function() {
					// 刷新页面
					window.location.href = "<?= admin_url('schedule/save_schedule_lesson?schedule_id=' . $schedule['id'] . '&class_id=' . $classInfo['id']) ?>";
				});
			});
		});
		// 初始化选择老师控件
		$("#select-teacher").selectTeacher({
			"multiSelect": false,
			"resultDom": $("[name=teacher_id]"),
		});
		// 初始化课程内容
		<?php if(isset($lessons) && !empty($lessons)): ?>
			<?php foreach($lessons as $lesson): ?>
				$(".table-lesson tbody tr:nth-child(<?= intval($lesson['schedule_class_index']) ?>) td:nth-child(<?= $lesson['schedule_week_index'] + 1 ?>)").html("\
					<?= $lesson['schedule_lesson'] ?>\
					<br/>\
					<?= $lesson['classroom_name'] ?>&nbsp;<?= $lesson['teacher_name'] ?>\
				").attr('data-lessonid', <?= $lesson['id'] ?>).attr('data-subject', '<?= $lesson['schedule_lesson'] ?>').attr('data-teacherid', <?= $lesson['schedule_teacher'] ?>).attr('data-teachername', '<?= $lesson['teacher_name'] ?>').attr('data-classroomid', <?= $lesson['schedule_room_id'] ?>);
			<?php endforeach ?>
		<?php endif ?>
	})
	function handleCoach() {
		// 任课老师与科目对应
		var subjectId = $("[name=subject] > option:selected").data("id") + '';
		// console.log(subjectId);
		$("[name=teacher] > option").each(function() {
			var subjectids = $(this).data("subjectids") + '';

			// var subjectids = '1,2';
			var subjectidarr = subjectids.split(",");
			// console.log(subjectidarr);
			var res = subjectidarr.indexOf(subjectId);
			// console.log(res);
			if(res == -1) {
				$(this).addClass('hidden');
			} else {
				$(this).removeClass('hidden');
			}
		});
	}
</script>